package com.hifar.test.unit.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hifar.common.QueryParamBean;
import com.hifar.sys.BusiResponse;
import com.hifar.sys.PageResult;
import com.hifar.sys.SystemUtil;
import com.hifar.test.unit.pojo.HfEnvUnitResult;
import com.hifar.test.unit.pojo.VO.HfEnvUnitResultQueryParam;
import com.hifar.test.unit.service.IHfEnvUnitResultService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author zhuWeiWei
 * @description TODO
 * @date 2025/1/14 17:20
 */
@RestController
@RequestMapping("/api/busi/test/envUnitResult")
@Api(tags = "项目试验结论")
public class HfEnvUnitResultController {

    @Autowired
    private IHfEnvUnitResultService unitResultService;

    @PostMapping("/add")
    @ApiOperation(value = "项目试验结论:新增")
    public BusiResponse add(@RequestBody @Validated HfEnvUnitResult hfEnvUnitResult) {
        unitResultService.save(hfEnvUnitResult);
        return BusiResponse.OK();
    }

    @PostMapping("/edit")
    @ApiOperation(value = "项目试验结论:编辑")
    public BusiResponse edit(@RequestBody @Validated HfEnvUnitResult hfEnvUnitResult) {
        unitResultService.updateById(hfEnvUnitResult);
        return BusiResponse.OK();
    }

    @PostMapping(value = "/listPage")
    @ApiOperation(value = "项目试验结论:分页查询列表")
    public BusiResponse listPage(@RequestBody @Validated HfEnvUnitResultQueryParam queryParamsVO) {
        QueryParamBean queryParamBean = new QueryParamBean<>(queryParamsVO, HfEnvUnitResult.class);
        QueryWrapper<HfEnvUnitResult> queryWrapper = queryParamBean.getQueryWrapper();
        String unitId = queryParamsVO.getUnitId();
        queryWrapper.lambda()
                .eq(HfEnvUnitResult::getUnitId, unitId);
        Page<HfEnvUnitResult> page = queryParamBean.getPage();
        Page<HfEnvUnitResult> pageData = unitResultService.page(page, queryWrapper);
        /* 计算分页信息返回给前端 */
        PageResult pageResult = SystemUtil.parsePage(page, pageData);
        return BusiResponse.OK(pageResult);

    }

    @GetMapping(value = "/list")
    @ApiOperation(value = "项目试验结论:查询全部")
    public BusiResponse listPage(@RequestParam("unitId") String unitId) {
        QueryWrapper<HfEnvUnitResult> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .select(HfEnvUnitResult::getId, HfEnvUnitResult::getName, HfEnvUnitResult::getTestResult)
                .eq(HfEnvUnitResult::getUnitId, unitId);
        List<HfEnvUnitResult> listData = unitResultService.list(queryWrapper);
        return BusiResponse.OK(listData);
    }


    @PostMapping("/delete")
    @ApiOperation(value = "项目结论:删除")
    public BusiResponse delete(@RequestBody List<String> idList) {
        QueryWrapper<HfEnvUnitResult> delete = new QueryWrapper<>();
        delete.in("id", idList);
        unitResultService.remove(delete);
        return BusiResponse.ok("删除成功！");
    }


}
